package cn.source.system.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import cn.source.common.constant.Constants;
import cn.source.common.core.domain.AjaxResult;
import cn.source.common.exception.base.BaseException;
import cn.source.common.utils.DateUtils;
import cn.source.common.utils.uuid.IdUtils;
import cn.source.system.domain.dto.OpenIdDto;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.stereotype.Service;
import cn.source.system.mapper.BuzMemberMapper;
import cn.source.system.domain.BuzMember;
import cn.source.system.service.IBuzMemberService;
import org.apache.commons.collections4.CollectionUtils;

import javax.annotation.Resource;

/**
 * 会员信息Service业务层处理
 *
 * @author sourcebyte.vip
 * @date 2024-05-01
 */
@Service
public class BuzMemberServiceImpl implements IBuzMemberService
{
    @Autowired
    private BuzMemberMapper buzMemberMapper;

    @Resource
    private AuthenticationManager authenticationManager;

    /**
     * 查询会员信息
     *
     * @param id 会员信息主键
     * @return 会员信息
     */
    @Override
    public BuzMember selectBuzMemberById(String id)
    {
        return buzMemberMapper.selectBuzMemberById(id);
    }

    /**
     * 查询会员信息列表
     *
     * @param buzMember 会员信息
     * @return 会员信息
     */
    @Override
    public List<BuzMember> selectBuzMemberList(BuzMember buzMember)
    {
        return buzMemberMapper.selectBuzMemberList(buzMember);
    }

    /**
     * 新增会员信息
     *
     * @param buzMember 会员信息
     * @return 结果
     */
    @Override
    public int insertBuzMember(BuzMember buzMember)
    {
        buzMember.setCreateTime(DateUtils.getNowDate());
        return buzMemberMapper.insertBuzMember(buzMember);
    }

    /**
     * 修改会员信息
     *
     * @param buzMember 会员信息
     * @return 结果
     */
    @Override
    public int updateBuzMember(BuzMember buzMember)
    {
        return buzMemberMapper.updateBuzMember(buzMember);
    }

    public int updateBuzMemberList(List<BuzMember> list){
        return buzMemberMapper.updateBuzMemberList(list);
    }

    /**
     * 批量删除会员信息
     *
     * @param ids 需要删除的会员信息主键
     * @return 结果
     */
    @Override
    public int deleteBuzMemberByIds(String[] ids)
    {
        return buzMemberMapper.deleteBuzMemberByIds(ids);
    }

    /**
     * 删除会员信息信息
     *
     * @param id 会员信息主键
     * @return 结果
     */
    @Override
    public int deleteBuzMemberById(String id)
    {
        return buzMemberMapper.deleteBuzMemberById(id);
    }

    @Override
    public AjaxResult h5CodeOneClickLogin(OpenIdDto openIdRequest) {
        AjaxResult ajax = AjaxResult.success();
        // 查询该openid是否登录
        BuzMember buzMember = new BuzMember();
        buzMember.setOpenid(openIdRequest.getOpenId());
        List<BuzMember> buzMembers = buzMemberMapper.selectBuzMemberList(buzMember);
        if(CollectionUtils.isNotEmpty(buzMembers)) {
            ajax.put("loginUser", buzMembers.get(0));
        }else{
            ajax.put("loginUser", null);
        }
        return  ajax;
    }
}
